package reporter

import (
	"xkginweb/global"
	"xkginweb/model/entity/reporter"
	"xkginweb/model/entity/reporter/request"
)

// 定义reporter的service提供reporter的数据curd的操作

type ReporterService struct{}

// @author: hcl
// @function: CreateReporter
// @description: 创建保修人员
// @param: reporter.QccReporter
// @return: err error

func (service *ReporterService) CreateReporter(reporter *reporter.QccReporter) (err error) {
	// 1： 获取数据的连接对象 如果执行成功err是nil，如果失败就把失败告诉
	if reporter.Birthdate == nil {
		err = global.KSD_DB.Omit("birthdate").Create(reporter).Error
	} else {
		err = global.KSD_DB.Create(reporter).Error
	}
	return err
}

//@author: hcl
//@function: UpdateReporter
//@description: 更新保修人员
//@param: reporter.QccReporter
//@return: err error

func (service *ReporterService) UpdateReporter(reporter *reporter.QccReporter) (err error) {
	//err = global.KSD_DB.Save(reporter).Error
	err = global.KSD_DB.Model(reporter).Updates(reporter).Error
	return err
}

// @author: hcl
// @function: DeleteReporter
// @description: 删除保修人员
// @param: reporter.QccReporter
// @return: err error

func (service *ReporterService) DeleteReporter(reporter *reporter.QccReporter) (err error) {
	err = global.KSD_DB.Delete(&reporter).Error
	return err
}

// @author: hcl
// @function: DeleteReporterById
// @description: 根据ID删除保修人员
// @param: uint
// @return: err error

func (service *ReporterService) DeleteReporterById(id uint) (err error) {
	var reporter reporter.QccReporter
	err = global.KSD_DB.Where("reporter_id = ?", id).Delete(&reporter).Error
	return err
}

// @author: hcl
// @function: GetReporter
// @description: 根据ID获取保修人员信息
// @param: id uint
// @return: reporter *reporter.QccReporter, err error

func (service *ReporterService) GetReporter(id uint) (reporter *reporter.QccReporter, err error) {
	err = global.KSD_DB.Where("reporter_id = ?", id).First(&reporter).Error
	return
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: LoadReporterPage
//@description: 分页获取保修人员列表
//@param: sysUserAuthorityID string, info request.PageInfo
//@return: list interface{}, total int64, err error

func (service *ReporterService) LoadReporterPage(info request.ReporterPageInfo) (list interface{}, total int64, err error) {
	// 获取分页的参数信息
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)

	// 准备查询那个数据库表
	db := global.KSD_DB.Model(&reporter.QccReporter{})

	// 准备切片帖子数组
	var ReporterList []reporter.QccReporter

	//if info.Status != -1 {
	//	db = db.Where("status = ?", info.Status)
	//}

	// 加条件
	if info.Keyword != "" {
		db = db.Where("(name like ?  )", "%"+info.Keyword+"%")
	}

	// 排序默时间降序降序
	db = db.Order("created_at")

	// 查询中枢
	err = db.Count(&total).Error
	if err != nil {
		return ReporterList, total, err
	} else {
		// 执行查询
		err = db.Limit(limit).Offset(offset).Find(&ReporterList).Error
	}

	// 结果返回
	return ReporterList, total, err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: LoadReporter
//@description: 获取保修人员列表
//@param: sysUserAuthorityID string, info request.PageInfo
//@return: list interface{}, total int64, err error

func (service *ReporterService) LoadReporter(info request.ReporterInfo) (list interface{}, err error) {

	// 准备查询那个数据库表
	db := global.KSD_DB.Model(&reporter.QccReporter{})

	// 准备切片帖子数组
	var ReporterList []reporter.QccReporter

	//if info.Status != -1 {
	//	db = db.Where("status = ?", info.Status)
	//}

	// 加条件
	if info.Keyword != "" {
		db = db.Where("(reporter_name like ?  )", "%"+info.Keyword+"%")
	}
	db = db.Where("(enable =  1 )")
	// 排序默时间降序降序
	db = db.Order("created_at")

	// 执行查询
	err = db.Find(&ReporterList).Error

	// 结果返回
	return ReporterList, err
}

// 用于登录
func (service *ReporterService) GetUserByAccount(account string) (reporter *reporter.QccReporter, err error) {
	// 根据account进行查询
	err = global.KSD_DB.Unscoped().Where("username = ?", account).First(&reporter).Error
	if err != nil {
		return nil, err
	}
	return reporter, nil
}
